package com.topscore.uke.ui.activities;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.squareup.timessquare.CalendarPickerView;
import com.topscore.uke.R;
import com.topscore.uke.entities.DateSelectorBean;
import com.topscore.uke.ui.ToolBarCompatActivity;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import butterknife.Bind;
import butterknife.ButterKnife;

public class RangeDateSelectorActivity extends ToolBarCompatActivity {

    @Bind(R.id.calendar)
    CalendarPickerView calendar;

    private DateSelectorBean mSelctedDateBean;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_confirm, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_confirm:
                onDateSelected();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_range_date_selector);
        ButterKnife.bind(this);
        mSelctedDateBean = getIntent().getParcelableExtra("select_dates");
        setCentralTitle("选择日期");
        try {
            initViews();
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }

    private void onDateSelected() {
        List<Date> selectedDates = calendar.getSelectedDates();
        if (selectedDates != null && !selectedDates.isEmpty()) {
            Date startDate = selectedDates.get(0);
            Date endDate = selectedDates.get(selectedDates.size() - 1);
//            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日");
//            Toast.makeText(mContext,
//                    "已选择"
//                            + "\t"
//                            + dateFormat.format(startDate)
//                            + "\t"
//                            + "至"
//                            + "\t"
//                            + dateFormat.format(endDate),
//                    Toast.LENGTH_SHORT)
//                    .show();
            Intent intent = new Intent();
            DateSelectorBean dateBean = new DateSelectorBean();
            dateBean.setStartDate(startDate);
            dateBean.setEndDate(endDate);
            intent.putExtra("select_dates", dateBean);
            setResult(RESULT_OK, intent);
            onBackPressed();
        } else {
            Toast.makeText(mContext, "请选择起止日期", Toast.LENGTH_SHORT).show();
        }
    }

    private void initViews() throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date beginDate = dateFormat.parse("2016-01-01");
        Date thisDate = new Date();
        Calendar today = Calendar.getInstance();
        today.setTime(thisDate);
        today.add(Calendar.DATE, 1);
        if (mSelctedDateBean == null) {
            calendar.init(beginDate, today.getTime())
                    .inMode(CalendarPickerView.SelectionMode.RANGE);
            calendar.scrollToDate(thisDate);
        } else {
            List<Date> dates = new ArrayList<>();
            dates.add(mSelctedDateBean.getStartDate());
            dates.add(mSelctedDateBean.getEndDate());
            calendar.init(beginDate,today.getTime())
            .inMode(CalendarPickerView.SelectionMode.RANGE)
            .withSelectedDates(dates);
        }


    }
}
